package com.cms.controller.admin;

import com.cms.domain.Feedback;
import com.cms.entity.Admin;
import com.cms.service.AdminService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;


/**
 * Controller - 个人资料
 * 
 * 
 * 
 */
@Controller("adminProfileController")
@RequestMapping("/admin/profile")
public class ProfileController extends BaseController {


	@Autowired
	private AdminService adminService;


	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(ModelMap modelMap) {
		modelMap.addAttribute("admin", adminService.getById(getCurrentAdmin().getId()));
		return getView("profile/edit");
	}

	/**
	 * 更新
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Feedback update(String currentPassword, String password) {
		Admin admin = getCurrentAdmin();
		Admin pAdmin = adminService.getById(admin.getId());
		if (StringUtils.isEmpty(currentPassword) || !StringUtils.equals(DigestUtils.md5Hex(currentPassword), pAdmin.getPassword())) {
			return Feedback.error("密码错误");
		}
		pAdmin.setPassword(DigestUtils.md5Hex(password));
		pAdmin.setUpdateDate(LocalDateTime.now());
		adminService.updateById(pAdmin);
		return Feedback.success(new HashMap<>());
	}

}